﻿using System;
using System.Collections.Generic;
using System.Text;
using Dukey.IDAL;
using Dukey.DBUtility;
using System.Data;
using System.Data.OleDb;

namespace AccessDAL
{
    /// <summary>
    /// 数据访问类Photos。
    /// </summary>
    public class Photos : IPhotos
    {
        public Photos()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Photos");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            return DbHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Dukey.Model.Photos model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Photos(");
            strSql.Append("title,brief,imgurl,bid,isshow,dots,px,iscomment,addtime,uptime,adminId,userId)");
            strSql.Append(" values (");
            strSql.Append("@title,@brief,@imgurl,@bid,@isshow,@dots,@px,@iscomment,@addtime,@uptime,@adminId,@userId)");
            OleDbParameter[] parameters = {					
					new OleDbParameter("@title", OleDbType.VarChar,50),
					new OleDbParameter("@brief", OleDbType.VarChar,255),
					new OleDbParameter("@imgurl", OleDbType.VarChar,255),
					new OleDbParameter("@bid", OleDbType.Integer,4),
					new OleDbParameter("@isshow", OleDbType.Boolean,1),
					new OleDbParameter("@dots", OleDbType.Integer,4),
					new OleDbParameter("@px", OleDbType.Integer,4),
					new OleDbParameter("@iscomment", OleDbType.Boolean,1),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@uptime", OleDbType.Date),
					new OleDbParameter("@adminId", OleDbType.Integer,4),
					new OleDbParameter("@userId", OleDbType.Integer,4)};         
            parameters[0].Value = model.title;
            parameters[1].Value = model.brief;
            parameters[2].Value = model.imgurl;
            parameters[3].Value = model.bid;
            parameters[4].Value = model.isshow;
            parameters[5].Value = model.dots;
            parameters[6].Value = model.px;
            parameters[7].Value = model.iscomment;
            parameters[8].Value = model.addtime;
            parameters[9].Value = model.uptime;
            parameters[10].Value = model.adminId;
            parameters[11].Value = model.userId;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Dukey.Model.Photos model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Photos set ");
            strSql.Append("ID=@ID,");
            strSql.Append("title=@title,");
            strSql.Append("brief=@brief,");
            strSql.Append("imgurl=@imgurl,");
            strSql.Append("bid=@bid,");
            strSql.Append("isshow=@isshow,");
            strSql.Append("dots=@dots,");
            strSql.Append("px=@px,");
            strSql.Append("iscomment=@iscomment,");
            strSql.Append("addtime=@addtime,");
            strSql.Append("uptime=@uptime,");
            strSql.Append("adminId=@adminId,");
            strSql.Append("userId=@userId");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4),
					new OleDbParameter("@title", OleDbType.VarChar,50),
					new OleDbParameter("@brief", OleDbType.VarChar,255),
					new OleDbParameter("@imgurl", OleDbType.VarChar,255),
					new OleDbParameter("@bid", OleDbType.Integer,4),
					new OleDbParameter("@isshow", OleDbType.Boolean,1),
					new OleDbParameter("@dots", OleDbType.Integer,4),
					new OleDbParameter("@px", OleDbType.Integer,4),
					new OleDbParameter("@iscomment", OleDbType.Boolean,1),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@uptime", OleDbType.Date),
					new OleDbParameter("@adminId", OleDbType.Integer,4),
					new OleDbParameter("@userId", OleDbType.Integer,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.title;
            parameters[2].Value = model.brief;
            parameters[3].Value = model.imgurl;
            parameters[4].Value = model.bid;
            parameters[5].Value = model.isshow;
            parameters[6].Value = model.dots;
            parameters[7].Value = model.px;
            parameters[8].Value = model.iscomment;
            parameters[9].Value = model.addtime;
            parameters[10].Value = model.uptime;
            parameters[11].Value = model.adminId;
            parameters[12].Value = model.userId;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Photos ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Dukey.Model.Photos GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,title,brief,imgurl,bid,isshow,dots,px,iscomment,addtime,uptime,adminId,userId from Photos ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            Dukey.Model.Photos model = new Dukey.Model.Photos();
            DataSet ds = DbHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.title = ds.Tables[0].Rows[0]["title"].ToString();
                model.brief = ds.Tables[0].Rows[0]["brief"].ToString();
                model.imgurl = ds.Tables[0].Rows[0]["imgurl"].ToString();
                if (ds.Tables[0].Rows[0]["bid"].ToString() != "")
                {
                    model.bid = int.Parse(ds.Tables[0].Rows[0]["bid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isshow"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isshow"].ToString() == "1") || (ds.Tables[0].Rows[0]["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["dots"].ToString() != "")
                {
                    model.dots = int.Parse(ds.Tables[0].Rows[0]["dots"].ToString());
                }
                if (ds.Tables[0].Rows[0]["px"].ToString() != "")
                {
                    model.px = int.Parse(ds.Tables[0].Rows[0]["px"].ToString());
                }
                if (ds.Tables[0].Rows[0]["iscomment"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["iscomment"].ToString() == "1") || (ds.Tables[0].Rows[0]["iscomment"].ToString().ToLower() == "true"))
                    {
                        model.iscomment = true;
                    }
                    else
                    {
                        model.iscomment = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["addtime"].ToString() != "")
                {
                    model.addtime = DateTime.Parse(ds.Tables[0].Rows[0]["addtime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["uptime"].ToString() != "")
                {
                    model.uptime = DateTime.Parse(ds.Tables[0].Rows[0]["uptime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adminId"].ToString() != "")
                {
                    model.adminId = int.Parse(ds.Tables[0].Rows[0]["adminId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["userId"].ToString() != "")
                {
                    model.userId = int.Parse(ds.Tables[0].Rows[0]["userId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,title,brief,imgurl,bid,isshow,dots,px,iscomment,addtime,uptime,adminId,userId ");
            strSql.Append(" FROM Photos ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelper.Query(strSql.ToString());
        }
        #endregion  成员方法

    }

}
